#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
#include <sys/vfs.h>

#include "getsysinfo.h"
#include "websocketserver.h"

#define CK_TIME 1

/*********************************************************************
 * @fn      pthread_sys_cpu_updata
 *
 * @brief   获取CPU数据线程函数 并通过websocket发送
 *
 * @param   none
 *
 * @return  none
 */
void *pthread_sys_cpu_updata(void *arg)
{
    char json[256];
    //获取CPU利用率
	int last_cpu_percent=-1,last_cpu_nprocs=-1;  //上一次CPU利用率
	int cpu_percent,cpu_nprocs;  //CPU利用率

	FILE * fp;
	char buf[128];
	char cpu[5];
	long int all1,all2,idle1,idle2;
	float usage;
	while(1)
    {
        fp = fopen("/proc/stat","r");
        if(fp!=NULL)
        {
            int user,nice,sys,idle,iowait,irq,softirq;
            fgets(buf,sizeof(buf),fp);
            sscanf(buf,"%s%d%d%d%d%d%d%d",cpu,&user,&nice,&sys,&idle,&iowait,&irq,&softirq);
            all1 = user+nice+sys+idle+iowait+irq+softirq;
            idle1 = idle;
            rewind(fp);
            //第二次读数据
            sleep(CK_TIME);
            memset(buf,0,sizeof(buf));
            cpu[0] = '\0';
            user=nice=sys=idle=iowait=irq=softirq=0;
            fgets(buf,sizeof(buf),fp);
            sscanf(buf,"%s%d%d%d%d%d%d%d",cpu,&user,&nice,&sys,&idle,&iowait,&irq,&softirq);
            all2 = user+nice+sys+idle+iowait+irq+softirq;
            idle2 = idle;
            usage = (float)((all2-all1)-(idle2-idle1))/(all2-all1) ;

            cpu_percent = usage*100; //处理器使用率
            if(cpu_percent>100)
            {
                printf("cpu_percent:%d\n", cpu_percent);
                cpu_percent=100;
            }
            cpu_nprocs = get_nprocs_conf();
            fclose(fp);
        }

        if(cpu_percent!=last_cpu_percent||cpu_nprocs!=last_cpu_nprocs)
        {
            sprintf(json, "{\"CPU_NPROCS\":%d,\"CPU_PERCENT\":%d}", cpu_nprocs, cpu_percent);
            last_cpu_percent = cpu_percent;
            last_cpu_nprocs  = cpu_nprocs;
            server_msg_str_update(json);
        }
        //sleep(1);
    }
}

/*********************************************************************
 * @fn      pthread_sys_ram_uptime_update
 *
 * @brief   获取ram和uptime数据线程函数 并通过websocket发送
 *
 * @param   none
 *
 * @return  none
 */
void *pthread_sys_ram_uptime_update(void *arg)
{
    char json[256];
    int last_mem_percent=-1,last_mem_total=-1,last_mem_free=-1; //上一次内存使用情况
    int mem_percent,mem_total,mem_free; //内存使用情况
    int uptimes; //系统启动多长时间
    int ret=-1;

    while(1)
    {
        //获取内存使用情况及系统启动时间
        struct sysinfo s_info;
        ret = sysinfo(&s_info);
        if(ret==0)
        {
            mem_total = s_info.totalram/(1024*1024);
            mem_free = s_info.freeram/(1024*1024);
            mem_percent = (float)(mem_total-mem_free)/mem_total*100;
            uptimes = s_info.uptime;
            if(last_mem_percent!=mem_percent||last_mem_total!=mem_total||last_mem_free!=mem_free)
            {
                sprintf(json, "{\"FREE_RAM\":%d,\"TOTAL_RAM\":%d,\"RAM_PERCENT\":%d,\"UPTIMES\":%d}", mem_free, mem_total, mem_percent, uptimes);
                last_mem_percent = mem_percent;
                last_mem_total   = mem_total;
                last_mem_free    = mem_free;
                server_msg_str_update(json);
            }
        }
        sleep(3);
    }
}

/*********************************************************************
 * @fn      pthread_sys_rootinfo_update
 *
 * @brief   获取根文件系统数据线程函数 并通过websocket发送
 *
 * @param   none
 *
 * @return  none
 */
void *pthread_sys_rootinfo_update(void *arg)
{
    char json[256];
    int last_root_percent=-1,last_root_total=-1,last_root_a=-1,last_root_use=-1; //上一次根文件系统使用情况
    int root_percent,root_total,root_a,root_use; //根文件系统使用情况

    while(1)
    {
        //获取根文件系统使用情况
        struct statfs sfs;
        int i = statfs("/",&sfs);
        root_percent = (sfs.f_blocks - sfs.f_bfree) *100 /(sfs.f_blocks - sfs.f_bfree + sfs.f_bavail)+1;
        root_total = 4*sfs.f_blocks/1024/1000;
        root_a = 4*sfs.f_bavail/1024/1000;
        root_use = 4*(sfs.f_blocks - sfs.f_bfree)/1024/1000;

        if(last_root_percent!=root_percent||last_root_total!=root_total||last_root_a!=root_a||last_root_use!=root_use)
        {
            sprintf(json, "{\"ROOT_SIZE\":%d,\"ROOT_A\":%d,\"ROOT_USE\":%d,\"ROOT_PERCENT\":%d}", root_total, root_a, root_use, root_percent);
            last_root_percent=root_percent;
            last_root_total=root_total;
            last_root_a=root_a;
            last_root_use=root_use;
            server_msg_str_update(json);
        }
        sleep(3);
    }
}
